package org.an.proj.contacts.net.service.retrofit.upload;

import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;
import okio.ForwardingSource;
import okio.Okio;
import okio.Source;

/**
 * 创建时间：2016年10月12日10:49 <br>
 * 作者：fuchaoyang <br>
 * 描述：
 */
public class ProgressResponseBody extends ResponseBody {

  private final ResponseBody responseBody;
  private final ProgressListener listener;
  private BufferedSource bufferedSource;

  public ProgressResponseBody(ResponseBody responseBody, ProgressListener listener) {
    this.responseBody = responseBody;
    this.listener = listener;
  }

  @Override public MediaType contentType() {
    return responseBody.contentType();
  }

  @Override public long contentLength() {
    return responseBody.contentLength();
  }

  @Override public BufferedSource source() {
    if (null == bufferedSource) {
      bufferedSource = Okio.buffer(source(responseBody.source()));
    }
    return bufferedSource;
  }

  private Source source(Source source) {
    return new ForwardingSource(source) {
      long totalBytesRead = 0L;

      @Override public long read(Buffer sink, long byteCount) throws IOException {
        long bytesRead = super.read(sink, byteCount);
        totalBytesRead += bytesRead != -1 ? bytesRead : 0;
        listener.onProgress(totalBytesRead, responseBody.contentLength(), bytesRead == -1);
        return bytesRead;
      }
    };
  }
}
